package com.weipai.springboot.module.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.model.sys.CheckCode;
import com.weipai.springboot.module.service.SmsService;
import com.weipai.springboot.module.util.SessionUserUtil;
import com.weipai.springboot.util.SmsHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author songmengqiang
 * @create 2018-09-13 17:28
 **/
@Service
public class SmsServiceImpl implements SmsService {

    private static final Logger logger = LoggerFactory.getLogger(SmsServiceImpl.class);

    private static final String CHECK_CODE_KEY_PREFIX = "check_code";

    private static final String CHECK_CODE_EXPIRE_DATE_KEY = "check_code_expire_date";

    private static final long TIME_OUT = 30 * 1000; //30 second  验证码30分有效

    private static final String PHONE_KEY = "phone";

    @Autowired
    private SessionUserUtil sessionUserUtil;

    @Override
    public Response sendCode(String phone) {
        Response response = new Response();
        //发送验证码
        String checkCode = null;
        try {
            checkCode = SmsHelper.sendCheckCode(phone);
            //将验证码放入session
//            setCheckCodeToSession(phone, checkCode);
            response.put("checkCode", checkCode);
            response.setCode(RetCode.SUCCESS);
        } catch (ClientException e) {
            response.setCode(RetCode.SMS_CODE_SEND_ERROR);
            logger.error("sendCheckCode to {} with error = {}", phone, e);
        }
        return response;
    }

    //从缓存获取验证码
    private void setCheckCodeToSession(String phone, String checkCode) {
        CheckCode code = new CheckCode(phone, checkCode, getExpireDate());
        sessionUserUtil.set(PHONE_KEY, code);
//        sessionUserUtil.set(PHONE_KEY, phone);
//        sessionUserUtil.set(CHECK_CODE_KEY_PREFIX + phone, checkCode);
//        sessionUserUtil.set(CHECK_CODE_EXPIRE_DATE_KEY, getExpireDate());
    }

    private CheckCode getCheckCode() {
        return sessionUserUtil.get(PHONE_KEY);
    }

    @Override
    public Response verifyCode(String code) {
        Response response = new Response();
        //获得session验证码
        CheckCode checkCode = getCheckCode();
        long current = System.currentTimeMillis();
        if (code != null && checkCode != null && code.equals(checkCode.getCheckCode())) {
            if (checkCode.getExpire() < current) {
                response.setCode(RetCode.SUCCESS);
            } else {
                response.setCode(RetCode.SMS_CODE_EXPIRE);
            }
            return response;
        } else {
            response.setCode(RetCode.SMS_CODE_VERIFY_FAIL);
            return response;
        }
    }

    private long getExpireDate() {
        return System.currentTimeMillis() + TIME_OUT;
    }
}
